package com.system.licence;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Properties;

public class SystemInfoTools {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		Runtime r = Runtime.getRuntime();
		Properties props = System.getProperties();
		InetAddress addr = null;
		try {
			addr = InetAddress.getLocalHost();
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String ip = addr.getHostAddress();

		Map<String, String> map = System.getenv();
		String userName = map.get("USERNAME");// 获取用户名
		String computerName = map.get("COMPUTERNAME");// 获取计算机名
		String userDomain = map.get("USERDOMAIN");// 获取计算机域名
		System.out.println("用户名:	" + userName);
		System.out.println("计算机名:	" + computerName);
		System.out.println("计算机域名:	" + userDomain);
		System.out.println("本地ip地址:	" + ip);
		System.out.println("本地主机名:	" + addr.getHostName());
		System.out.println("JVM可以使用的总内存:	" + r.totalMemory());
		System.out.println("JVM可以使用的剩余内存:	" + r.freeMemory());
		System.out.println("JVM可以使用的处理器个数:	" + r.availableProcessors());
		System.out.println("Java的运行环境版本：	" + props.getProperty("java.version"));
		System.out.println("Java的运行环境供应商：	" + props.getProperty("java.vendor"));
		System.out.println("Java供应商的URL：	"
				+ props.getProperty("java.vendor.url"));
		System.out.println("Java的安装路径：	" + props.getProperty("java.home"));
		System.out.println("Java的虚拟机规范版本：	"
				+ props.getProperty("java.vm.specification.version"));
		System.out.println("Java的虚拟机规范供应商：	"
				+ props.getProperty("java.vm.specification.vendor"));
		System.out.println("Java的虚拟机规范名称：	"
				+ props.getProperty("java.vm.specification.name"));
		System.out.println("Java的虚拟机实现版本：	"
				+ props.getProperty("java.vm.version"));
		System.out.println("Java的虚拟机实现供应商：	"
				+ props.getProperty("java.vm.vendor"));
		System.out
				.println("Java的虚拟机实现名称：	" + props.getProperty("java.vm.name"));
		System.out.println("Java运行时环境规范版本：	"
				+ props.getProperty("java.specification.version"));
		System.out.println("Java运行时环境规范供应商：	"
				+ props.getProperty("java.specification.vender"));
		System.out.println("Java运行时环境规范名称：	"
				+ props.getProperty("java.specification.name"));
		System.out.println("Java的类格式版本号：	"
				+ props.getProperty("java.class.version"));
		System.out.println("Java的类路径：	" + props.getProperty("java.class.path"));
		System.out.println("加载库时搜索的路径列表：	"
				+ props.getProperty("java.library.path"));
		System.out.println("默认的临时文件路径：	" + props.getProperty("java.io.tmpdir"));
		System.out.println("一个或多个扩展目录的路径：	"
				+ props.getProperty("java.ext.dirs"));
		System.out.println("操作系统的名称：	" + props.getProperty("os.name"));
		System.out.println("操作系统的构架：	" + props.getProperty("os.arch"));
		System.out.println("操作系统的版本：	" + props.getProperty("os.version"));
		System.out.println("文件分隔符：	" + props.getProperty("file.separator"));
		System.out.println("路径分隔符：	" + props.getProperty("path.separator"));
		System.out.println("行分隔符：	" + props.getProperty("line.separator"));
		System.out.println("用户的账户名称：	" + props.getProperty("user.name"));
		System.out.println("用户的主目录：	" + props.getProperty("user.home"));
		System.out.println("用户的当前工作目录：	" + props.getProperty("user.dir"));
		// TODO Auto-generated method stub
		// 获取当前系统名称
		System.out.println("当前系统名称："
				+ System.getProperty("os.name").toLowerCase());
		// 获取当前系统語言
		System.out.println("当前系统語言："
				+ System.getProperty("user.language").toLowerCase());
		// 获取当前系统版本
		System.out.println("当前系统版本："
				+ System.getProperty("os.version").toLowerCase());
		// 获取当前系统架構
		System.out.println("当前系统架構："
				+ System.getProperty("os.arch").toLowerCase());

		String os = System.getProperty("os.name").toLowerCase();
		if (os.startsWith("windows")) {
			String mac = getWindowsMACAddress();
			System.out.println("本地是windows:" + mac);
		} else if (os.startsWith("linux")) {
			String mac = getLinuxMACAddress();
			System.out.println("本地是Linux系统,MAC地址是:" + mac);
		} else {
			String mac = getUnixMACAddress();
			System.out.println("本地是Unix系统 MAC地址是:" + mac);
		}

		System.out.println("unix系统MAC：" + getUnixMACAddress());

		System.out.println("windows系统MAC：" + getWindowsMACAddress());

		try {
			InetAddress a = InetAddress.getLocalHost();
			if (a != null) {

				System.out.println("**********IP信息：" + a.getHostName() + "   "
						+ a.getHostAddress() + "  " + a.getCanonicalHostName()
						+ "" + a.getAddress());

			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String getSysMACAddress() {
		String mac = "";
		String os = System.getProperty("os.name").toLowerCase();
		try {
			if (os.startsWith("windows")) {
				mac = getWindowsMACAddress();
			} else if (os.startsWith("linux")) {
				mac = getLinuxMACAddress();
			} else {
				mac = getUnixMACAddress();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mac;
	}

	public static String getSysIPAddress() throws UnknownHostException {
		InetAddress addr = null;
		addr = InetAddress.getLocalHost();
		return addr.getHostAddress();
	}

	public static String getUnixMACAddress() throws Exception {
		String mac = "";
		BufferedReader bufferedReader = null;
		Process process = null;
		try {
			process = Runtime.getRuntime().exec("ifconfig");
			bufferedReader = new BufferedReader(new InputStreamReader(process
					.getInputStream()));
			String line = null;
			int index = -1;
			while ((line = bufferedReader.readLine()) != null) {
				index = line.toLowerCase().indexOf("hwaddr");
				if (index >= 0) {
					mac = line.substring(index + "hwaddr".length() + 1)
							.toString();
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (process != null) {
				process.destroy();
			}
		}
		return mac;
	}

	public static String getWindowsMACAddress() throws Exception {
		String mac = "";
		BufferedReader bufferedReader = null;
		Process process = null;
		try {
			process = Runtime.getRuntime().exec("ipconfig -all");
			bufferedReader = new BufferedReader(new InputStreamReader(process
					.getInputStream()));
			String line = null;
			int index = -1;
			while ((line = bufferedReader.readLine()) != null) {
				index = line.toLowerCase().indexOf("physical address");
				if (index >= 0) {
					index = line.toLowerCase().indexOf(":");
					if (index >= 0) {
						mac = line.substring(index + 1).toString();
					}
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (process != null) {
				process.destroy();
			}
		}
		return mac;
	}

	public static String getLinuxMACAddress() throws Exception {
		String mac = "";
		BufferedReader bufferedReader = null;
		Process process = null;
		try {
			process = Runtime.getRuntime().exec("ifconfig eth0");
			bufferedReader = new BufferedReader(new InputStreamReader(process
					.getInputStream()));
			String line = null;
			int index = -1;
			while ((line = bufferedReader.readLine()) != null) {
				index = line.toLowerCase().indexOf("硬件地址");
				if (index >= 0) {
					mac = line.substring(index + 4).toString();
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (process != null) {
				process.destroy();
			}
		}
		return mac;
	}

	public static String getWinxMACAddress() throws Exception {
		StringBuffer mac = new StringBuffer();
		try {
			InetAddress ia = InetAddress.getLocalHost();
			byte[] macs = NetworkInterface.getByInetAddress(ia)
					.getHardwareAddress();
			for (int i = 0; i < mac.length(); i++) {
				if (i != 0) {
					mac.append("-");
				}
				String s = Integer.toHexString(macs[i] & 0xFF);
				mac.append(s.length() == 1 ? 0 : 0 + s);
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return mac.toString().toUpperCase();
	}
}
